import path from 'path'
import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import eslintPlugin from 'vite-plugin-eslint'
import {viteMockServe} from 'vite-plugin-mock'

import {server} from './config'

export default defineConfig({
    base: './',
    resolve: {
        alias: {
            '@': path.resolve(__dirname, './src'),
        }
    },
    plugins: [
        vue(),
        viteMockServe(),
        eslintPlugin({
            include: ['src/**/*.js', 'src/**/*.vue', 'src/**/*.ts'],
            fix: true
        }),
    ],
    css: {
        preprocessorOptions: {
            scss: {
                additionalData: `@import "./src/assets/style/variables.scss";`
            }
        }
    },
    server,
    build: {
        rollupOptions: {
            output: {
                manualChunks(id: string) {
                    if (id.includes('node_modules')) {
                        let prefix = ''
                        const fileName = id.split('node_modules/')[1].split('/')
                        if (/.*\.(js|mjs|css)(\?|$)/.test(id)) {
                            prefix = RegExp.$1 === 'css' ? 'css/' : 'js/'
                        }
                        return prefix + fileName[0].replace('@', '')
                    }
                }
            }
        }
    }
})
